package com.common.domainService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.CommentForm;
import com.common.DAO.ICommentDAO;
import com.common.DAO.ICovoitureurDAO;
import com.common.entity.Comment;
import com.common.transformer.CommentTransformer;

public class CommentService implements ICommentService {
	
	Logger logger = Logger.getLogger(CommentService.class);

	private ICommentDAO commentDAO;
	private ICovoitureurDAO covoitureurDAO;

	@Override
	public List<CommentForm> getAllComments() {
		List<CommentForm> listCommentForm =new ArrayList<CommentForm>();
		try{
			
			List<Comment> listComment=commentDAO.list();
			if(listComment!=null)
			//Transformation de la listAdmin en listAdminForm pour action
			for(Comment co : listComment){
				String name=covoitureurDAO.getCovoitureur(co.getSender_covoit()).getNom()+" "+covoitureurDAO.getCovoitureur(co.getSender_covoit()).getPrenom();
				listCommentForm.add(CommentTransformer.Comment_TO_CommentForm(co,name));
			}
		}catch (Exception e) {
			logger.error("Erreur methode list de PathSerivce",e);
			return null;
		}
		return listCommentForm;
	}

	@Override
	public List<CommentForm> getPathComments(int path_id) {
		List<CommentForm> listCommentForm =new ArrayList<CommentForm>();
		try{
			
			List<Comment> listComment=commentDAO.listPathComment(path_id);
			if(listComment!=null)
			//Transformation de la listAdmin en listAdminForm pour action
			for(Comment co : listComment){
				String name=covoitureurDAO.getCovoitureur(co.getSender_covoit()).getNom()+" "+covoitureurDAO.getCovoitureur(co.getSender_covoit()).getPrenom();
				listCommentForm.add(CommentTransformer.Comment_TO_CommentForm(co,name));
			}
		}catch (Exception e) {
			logger.error("Erreur methode list de PathSerivce",e);
			return null;
		}
		return listCommentForm;
	}

	@Override
	public List<CommentForm> getApplicationComments() {
		List<CommentForm> listCommentForm =new ArrayList<CommentForm>();
		try{
			
			List<Comment> listComment=commentDAO.listApplicationComment();
			if(listComment!=null)
			//Transformation de la listAdmin en listAdminForm pour action
			for(Comment co : listComment){
				String name=covoitureurDAO.getCovoitureur(co.getSender_covoit()).getNom()+" "+covoitureurDAO.getCovoitureur(co.getSender_covoit()).getPrenom();
				listCommentForm.add(CommentTransformer.Comment_TO_CommentForm(co,name));
			}
		}catch (Exception e) {
			logger.error("Erreur methode list de PathSerivce",e);
			return null;
		}
		return listCommentForm;
	}

	@Override
	public List<CommentForm> getCovoitureurComments(int covoit_id) {
		List<CommentForm> listCommentForm =new ArrayList<CommentForm>();
		try{
			
			List<Comment> listComment=commentDAO.listCovoitureurComment(covoit_id);
			if(listComment!=null)
			//Transformation de la listAdmin en listAdminForm pour action
			for(Comment co : listComment){
				String name=covoitureurDAO.getCovoitureur(co.getSender_covoit()).getNom()+" "+covoitureurDAO.getCovoitureur(co.getSender_covoit()).getPrenom();
				listCommentForm.add(CommentTransformer.Comment_TO_CommentForm(co,name));
			}
		}catch (Exception e) {
			logger.error("Erreur methode list de PathSerivce",e);
			return null;
		}
		return listCommentForm;
	}

	@Override
	public void addPathComment(CommentForm coForm,int id_covoit,int id_path) {
		coForm.setReceiver_covoit(0);
		coForm.setDate_comment(new Date());
		coForm.setSender_covoit(id_covoit);
		coForm.setPath_id(id_path);
		try{
			Comment co=CommentTransformer.CommentForm_TO_Comment(coForm);
			commentDAO.save(co);
		}catch (Exception e) {
			logger.error("An error has occured in CommentService while saving", e);
		}
	}

	@Override
	public void addCovoitureurComment(CommentForm coForm,int id_covoit,int id_receiver_covoit) {
		coForm.setPath_id(0);
		coForm.setReceiver_covoit(id_receiver_covoit);
		coForm.setDate_comment(new Date());
		coForm.setSender_covoit(id_covoit);
		try{
			Comment co=CommentTransformer.CommentForm_TO_Comment(coForm);
			commentDAO.save(co);
		}catch (Exception e) {
			logger.error("An error has occured in CommentService while saving", e);
		}
	}

	@Override
	public void addApplicationComment(CommentForm coForm,int id_covoit) {
		coForm.setPath_id(0);
		coForm.setReceiver_covoit(0);
		coForm.setSender_covoit(id_covoit);
		coForm.setDate_comment(new Date());
		try{
			Comment co=CommentTransformer.CommentForm_TO_Comment(coForm);
			commentDAO.save(co);
		}catch (Exception e) {
			logger.error("An error has occured in CommentService while saving", e);
		}
	}
	
	public ICommentDAO getCommentDAO() {
		return commentDAO;
	}

	public void setCommentDAO(ICommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}
	
	public ICovoitureurDAO getCovoitureurDAO() {
		return covoitureurDAO;
	}

	public void setCovoitureurDAO(ICovoitureurDAO covoitureurDAO) {
		this.covoitureurDAO = covoitureurDAO;
	}
}
